
package server.quest;


import client.MapleCharacter;
import client.MapleQuestStatus;
import com.alibaba.druid.pool.DruidPooledConnection;
import configs.ServerConfig;
import constants.JobConstants;
import database.DatabaseConnection;
import handling.opcode.EffectOpcode;
import scripting.quest.QuestScriptManager;
import tools.Pair;
import tools.packet.EffectPacket;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;




















 public class MapleQuest implements Serializable {
    /*  23 */   private static final List<MapleQuest> BulbQuest = new LinkedList<>();
    private static final long serialVersionUID = 9179541993413738569L;
    /*  24 */   private static final Map<Integer, MapleQuest> quests = new LinkedHashMap<>();
    /*  25 */   protected final List<MapleQuestRequirement> startReqs = new LinkedList<>();
    /*  26 */   protected final List<MapleQuestRequirement> completeReqs = new LinkedList<>();
    /*  27 */   protected final List<MapleQuestAction> startActs = new LinkedList<>();
    /*  28 */   protected final List<MapleQuestAction> completeActs = new LinkedList<>();
    /*  29 */   protected final Map<String, List<Pair<String, Pair<String, Integer>>>> partyQuestInfo = new LinkedHashMap<>();
    /*  30 */   protected final Map<Integer, Integer> relevantMobs = new LinkedHashMap<>();
    /*  31 */   protected final Map<Integer, Integer> questItems = new LinkedHashMap<>();
       protected int id;
    /*  33 */   protected String name = "";
    private boolean autoStart = false;
    private boolean autoPreComplete = false;
    private boolean repeatable = false;
       private boolean customend = false;
    /*  35 */   private int viewMedalItem = 0, selectedSkillID = 0;
    private boolean blocked = false;
    private boolean autoAccept = false;
    private boolean autoComplete = false;
       private boolean scriptedStart = false;
       private boolean selfStart = false;



    public MapleQuest() {
    }



    protected MapleQuest(int id) {
        /*  42 */
        this.id = id;

    }



    private static MapleQuest loadQuest(ResultSet rs, PreparedStatement psr, PreparedStatement psa, PreparedStatement pss, PreparedStatement psq, PreparedStatement psi, PreparedStatement psp) throws SQLException {
        /*  46 */
        MapleQuest ret = new MapleQuest(rs.getInt("questid"));
        /*  47 */
        ret.name = rs.getString("name");
        /*  48 */
        ret.autoStart = (rs.getInt("autoStart") > 0);
        /*  49 */
        ret.autoPreComplete = (rs.getInt("autoPreComplete") > 0);
        /*  50 */
        ret.autoAccept = (rs.getInt("autoAccept") > 0);
        /*  51 */
        ret.autoComplete = (rs.getInt("autoComplete") > 0);
        /*  52 */
        ret.viewMedalItem = rs.getInt("viewMedalItem");
        /*  53 */
        ret.selectedSkillID = rs.getInt("selectedSkillID");
        /*  54 */
        ret.blocked = (rs.getInt("blocked") > 0);

        /*  56 */
        ret.selfStart = (rs.getInt("selfStart") > 0);

        /*  58 */
        psr.setInt(1, ret.id);
        /*  59 */
        ResultSet rse = psr.executeQuery();
        /*  60 */
        while (rse.next()) {
            /*  61 */
            MapleQuestRequirementType type = MapleQuestRequirementType.getByWZName(rse.getString("name"));
            /*  62 */
            MapleQuestRequirement req = new MapleQuestRequirement(ret, type, rse);
            /*  63 */
            if (type.equals(MapleQuestRequirementType.interval)) {
                /*  64 */
                ret.repeatable = true;
                /*  65 */
            } else if (type.equals(MapleQuestRequirementType.normalAutoStart)) {
                /*  66 */
                ret.repeatable = true;
                /*  67 */
                ret.autoStart = true;
                /*  68 */
            } else if (type.equals(MapleQuestRequirementType.startscript)) {
                /*  69 */
                ret.scriptedStart = true;
                /*  70 */
            } else if (type.equals(MapleQuestRequirementType.endscript)) {
                /*  71 */
                ret.customend = true;
                /*  72 */
            } else if (type.equals(MapleQuestRequirementType.mob)) {
                /*  73 */
                for (Pair<Integer, Integer> mob : req.getDataStore()) {
                    /*  74 */
                    ret.relevantMobs.put(mob.left, mob.right);

                }
                /*  76 */
            } else if (type.equals(MapleQuestRequirementType.item)) {
                /*  77 */
                for (Pair<Integer, Integer> it : req.getDataStore()) {
                    /*  78 */
                    ret.questItems.put(it.left, it.right);

                }

            }
            /*  81 */
            if (rse.getInt("type") == 0) {
                /*  82 */
                ret.startReqs.add(req);

            } else {
                /*  84 */
                ret.completeReqs.add(req);

            }
            /*  86 */
            if (ret.isSelfStart()) {
                /*  87 */
                BulbQuest.add(ret);

            }

        }
        /*  90 */
        rse.close();

        /*  92 */
        psa.setInt(1, ret.id);
        /*  93 */
        rse = psa.executeQuery();
        /*  94 */
        while (rse.next()) {
            /*  95 */
            MapleQuestActionType ty = MapleQuestActionType.getByWZName(rse.getString("name"));
            /*  96 */
            if (rse.getInt("type") == 0) {
                /*  97 */
                if (ty == MapleQuestActionType.item && ret.id == 7103) {

                    continue;

                }
                /* 100 */
                ret.startActs.add(new MapleQuestAction(ty, rse, ret, pss, psq, psi));
                continue;

            }
            /* 102 */
            if (ty == MapleQuestActionType.item && ret.id == 7102) {

                continue;

            }
            /* 105 */
            ret.completeActs.add(new MapleQuestAction(ty, rse, ret, pss, psq, psi));

        }

        /* 108 */
        rse.close();

        /* 110 */
        psp.setInt(1, ret.id);
        /* 111 */
        rse = psp.executeQuery();
        /* 112 */
        while (rse.next()) {
            /* 113 */
            if (!ret.partyQuestInfo.containsKey(rse.getString("rank"))) {
                /* 114 */
                ret.partyQuestInfo.put(rse.getString("rank"), new ArrayList<>());

            }
            /* 116 */
            ret.partyQuestInfo.get(rse.getString("rank")).add(new Pair<>(rse.getString("mode"), new Pair<>(rse.getString("property"), rse.getInt("value"))));

        }
        /* 118 */
        rse.close();
        /* 119 */
        return ret;

    }



    public static void initQuests() {

        try {
            /* 124 */
            DruidPooledConnection con = DatabaseConnection.getInstance().getConnection();
            /* 125 */
            PreparedStatement ps = con.prepareStatement("SELECT * FROM wz_questdata");
            /* 126 */
            PreparedStatement psr = con.prepareStatement("SELECT * FROM wz_questreqdata WHERE questid = ?");
            /* 127 */
            PreparedStatement psa = con.prepareStatement("SELECT * FROM wz_questactdata WHERE questid = ?");
            /* 128 */
            PreparedStatement pss = con.prepareStatement("SELECT * FROM wz_questactskilldata WHERE uniqueid = ?");
            /* 129 */
            PreparedStatement psq = con.prepareStatement("SELECT * FROM wz_questactquestdata WHERE uniqueid = ?");
            /* 130 */
            PreparedStatement psi = con.prepareStatement("SELECT * FROM wz_questactitemdata WHERE uniqueid = ?");
            /* 131 */
            PreparedStatement psp = con.prepareStatement("SELECT * FROM wz_questpartydata WHERE questid = ?");
            /* 132 */
            ResultSet rs = ps.executeQuery();
            /* 133 */
            while (rs.next()) {
                /* 134 */
                quests.put(Integer.valueOf(rs.getInt("questid")), loadQuest(rs, psr, psa, pss, psq, psi, psp));

            }
            /* 136 */
            rs.close();
            /* 137 */
            ps.close();
            /* 138 */
            psr.close();
            /* 139 */
            psa.close();
            /* 140 */
            pss.close();
            /* 141 */
            psq.close();
            /* 142 */
            psi.close();
            /* 143 */
            psp.close();
            /* 144 */
        } catch (Exception e) {
            /* 145 */
            e.printStackTrace();

        }


        /* 149 */
        System.out.println("共加载 " + quests.size() + " 个任务信息.");
        /* 150 */
        System.out.println("共加载 " + BulbQuest.size() + " 个灯泡任务,角色登录后会自动屏蔽的!");

    }




    public static MapleQuest getInstance(int id) {
        /* 155 */
        return quests.computeIfAbsent(Integer.valueOf(id), MapleQuest::new);

    }



    public static Collection<MapleQuest> getAllInstances() {
        /* 159 */
        return quests.values();

    }








    public static List<MapleQuest> GetBulbQuest() {
        /* 168 */
        return BulbQuest;

    }



    public List<Pair<String, Pair<String, Integer>>> getInfoByRank(String rank) {
        /* 172 */
        return this.partyQuestInfo.get(rank);

    }



    public boolean isPartyQuest() {
        /* 176 */
        return (this.partyQuestInfo.size() > 0);

    }



    public int getSkillID() {
        /* 180 */
        return this.selectedSkillID;

    }



    public String getName() {
        /* 184 */
        return this.name;

    }



    public List<MapleQuestAction> getCompleteActs() {
        /* 188 */
        return this.completeActs;

    }



    public boolean canStart(MapleCharacter chr, Integer npcid) {
        /* 192 */
        if (chr.getQuest(this).getStatus() != 0 && (chr.getQuest(this).getStatus() != 2 || !this.repeatable)) {
            /* 193 */
            if (chr.isShowPacket()) {
                /* 194 */
                chr.dropMessage(6, "开始任务 canStart: " + ((chr.getQuest(this).getStatus() != 0) ? 1 : 0) + " - " + ((chr.getQuest(this).getStatus() != 2 || !this.repeatable) ? 1 : 0) + " repeatable: " + this.repeatable);

            }
            /* 196 */
            forceComplete(chr, npcid.intValue());
            /* 197 */
            return false;

        }
        /* 199 */
        if (this.blocked && !chr.isGM()) {
            /* 200 */
            if (chr.isShowPacket()) {
                /* 201 */
                chr.dropMessage(6, "开始任务 canStart - blocked " + this.blocked);

            }
            /* 203 */
            return false;

        }



        /* 208 */
        for (MapleQuestRequirement r : this.startReqs) {
            /* 209 */
            if (r.getType() == MapleQuestRequirementType.dayByDay && npcid != null) {
                /* 210 */
                forceComplete(chr, npcid.intValue());
                /* 211 */
                return false;

            }
            /* 213 */
            if (!r.check(chr, npcid)) {
                /* 214 */
                if (chr.isShowPacket()) {
                    /* 215 */
                    chr.dropMessage(6, "开始任务 canStart - check " + (!r.check(chr, npcid) ? 1 : 0));

                }
                /* 217 */
                return false;

            }

        }
        /* 220 */
        return true;

    }



    public boolean canComplete(MapleCharacter chr, Integer npcid) {
        /* 224 */
        if (chr.getQuest(this).getStatus() != 1) {
            /* 225 */
            return false;

        }
        /* 227 */
        if (this.blocked && !chr.isGM()) {
            /* 228 */
            return false;

        }
        /* 230 */
        if (this.autoComplete && npcid != null && this.viewMedalItem <= 0) {
            /* 231 */
            forceComplete(chr, npcid.intValue());
            /* 232 */
            return false;

        }
        /* 234 */
        for (MapleQuestRequirement r : this.completeReqs) {
            /* 235 */
            if (!r.check(chr, npcid)) {
                /* 236 */
                return false;

            }

        }
        /* 239 */
        return true;

    }



    public void RestoreLostItem(MapleCharacter chr, int itemid) {
        /* 243 */
        if (this.blocked && !chr.isGM()) {

            return;

        }
        /* 246 */
        for (MapleQuestAction a : this.startActs) {
            /* 247 */
            if (a.RestoreLostItem(chr, itemid)) {

                break;

            }

        }

    }

































    public void start(MapleCharacter chr, int npc) {
        /* 284 */
        if (chr.isShowPacket() && ServerConfig.WORLD_SHOWQUESTINFO) {
            /* 285 */
            chr.dropMessage(6, "[任务系统] 开始任务 NPC: " + npc + " 自动开始：" + this.autoStart + " 检查地图:：" + checkNPCOnMap(chr, npc) + " 可执行:：" + canStart(chr, Integer.valueOf(npc)));

        }
        /* 287 */
        if ((this.autoStart || checkNPCOnMap(chr, npc)) && canStart(chr, Integer.valueOf(npc))) {
            /* 288 */
            for (MapleQuestAction a : this.startActs) {
                /* 289 */
                if (!a.checkEnd(chr, null)) {
                    /* 290 */
                    if (chr.isShowPacket()) {
                        /* 291 */
                        chr.dropMessage(6, "开始任务 checkEnd 错误...");

                    }

                    return;

                }

            }
            /* 296 */
            for (MapleQuestAction a : this.startActs) {
                /* 297 */
                a.runStart(chr, null);

            }
            /* 299 */
            if (!this.customend) {
                /* 300 */
                forceStart(chr, npc, null);

            } else {
                /* 302 */
                QuestScriptManager.getInstance().startQuest(chr.getClient(), npc, getId());

            }

        }

    }



    public void complete(MapleCharacter chr, int npc) {
        /* 308 */
        complete(chr, npc, null);

    }



    public void complete(MapleCharacter chr, int npc, Integer selection) {
        /* 312 */
        if (chr.getMap() != null && (this.autoPreComplete || checkNPCOnMap(chr, npc)) && canComplete(chr, Integer.valueOf(npc))) {
            /* 313 */
            for (MapleQuestAction a : this.completeActs) {
                /* 314 */
                if (!a.checkEnd(chr, selection)) {

                    return;

                }

            }
            /* 318 */
            forceComplete(chr, npc);
            /* 319 */
            for (MapleQuestAction a : this.completeActs) {
                /* 320 */
                a.runEnd(chr, selection);

            }
            /* 322 */
            chr.send(EffectPacket.showSpecialEffect(EffectOpcode.UserEffect_QuestComplete.getValue()));
            /* 323 */
            chr.getMap().broadcastMessage(chr, EffectPacket.showForeignEffect(chr.getId(), EffectOpcode.UserEffect_QuestComplete.getValue()), false);

        }

    }



    public void forfeit(MapleCharacter chr) {
        /* 328 */
        if (chr.getQuest(this).getStatus() != 1) {

            return;

        }
        /* 331 */
        MapleQuestStatus oldStatus = chr.getQuest(this);
        /* 332 */
        MapleQuestStatus newStatus = new MapleQuestStatus(this, 0);
        /* 333 */
        newStatus.setForfeited(oldStatus.getForfeited() + 1);
        /* 334 */
        newStatus.setCompletionTime(oldStatus.getCompletionTime());
        /* 335 */
        chr.updateQuest(newStatus);

    }



    public void forceStart(MapleCharacter chr, int npc, String customData) {
        /* 339 */
        if (chr.isShowPacket()) {
            /* 340 */
            chr.dropSpouseMessage(20, "[Start] 开始任务 任务ID： " + getId() + " 任务Npc: " + npc);

        }
        /* 342 */
        MapleQuestStatus newStatus = new MapleQuestStatus(this, (byte) 1, npc);
        /* 343 */
        newStatus.setForfeited(chr.getQuest(this).getForfeited());
        /* 344 */
        newStatus.setCompletionTime(chr.getQuest(this).getCompletionTime());
        /* 345 */
        newStatus.setCustomData(customData);
        /* 346 */
        chr.updateQuest(newStatus);

    }



    public void forceComplete(MapleCharacter chr, int npc) {
        /* 350 */
        if (chr.isShowPacket()) {
            /* 351 */
            chr.dropSpouseMessage(20, "[Complete] 完成任务 任务ID： " + getId() + " 任务Npc: " + npc);

        }
        /* 353 */
        MapleQuestStatus newStatus = new MapleQuestStatus(this, (byte) 2, npc);
        /* 354 */
        newStatus.setForfeited(chr.getQuest(this).getForfeited());
        /* 355 */
        chr.updateQuest(newStatus);

    }



    public int getId() {
        /* 359 */
        return this.id;

    }



    public Map<Integer, Integer> getRelevantMobs() {
        /* 363 */
        return this.relevantMobs;

    }



    private boolean checkNPCOnMap(MapleCharacter player, int npcId) {
        /* 367 */
        return ((JobConstants.is龙神(player.getJob()) && npcId == 1013000) || (
                /* 368 */       JobConstants.is恶魔猎手(player.getJob()) && npcId == 0) || (
                /* 369 */       JobConstants.is双弩精灵(player.getJob()) && npcId == 0) || npcId == 2151009 || npcId == 3000018 || npcId == 9010000 || (npcId >= 2161000 && npcId <= 2161011) || npcId == 9000040 || npcId == 9000066 || npcId == 2010010 || npcId == 1032204 || npcId == 0 || npcId == 2182001 || npcId == 9000279 || npcId == 9000371 || (player












/* 382 */.getMap() != null && player.getMap().containsNPC(npcId)));

    }



    public int getMedalItem() {
        /* 386 */
        return this.viewMedalItem;

    }



    public boolean isBlocked() {
        /* 390 */
        return this.blocked;

    }



    public int getAmountofItems(int itemId) {
        /* 394 */
        return (this.questItems.get(Integer.valueOf(itemId)) != null) ? ((Integer) this.questItems.get(Integer.valueOf(itemId))).intValue() : 0;

    }



    public boolean hasStartScript() {
        /* 398 */
        return this.scriptedStart;

    }



    public boolean hasEndScript() {
        /* 402 */
        return this.customend;

    }








    public boolean isSelfStart() {
        /* 411 */
        return this.selfStart;

    }

}


/* Location:              C:\PGX\Server.jar!\server\quest\MapleQuest.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */